﻿using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.Repository;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;
using Net5CommonLib.SystemLib.TypeTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using VueAndNetCoreEfApi.DtoModel.SystemAdmin;
using System.Runtime.Intrinsics.Arm;

namespace VueAndNetCoreEfApi.Repository.SystemAdmin
{
    public class OutsourceRepository:BaseRepository, IOutsourceRepository
    {
        private IRoleDbRepository _iroleDbRepository;

        public OutsourceRepository(IRoleDbRepository iroleDbRepository)
        {
            _iroleDbRepository = iroleDbRepository;
        }
        public PagerResult<OutsourcePo> GetPageList(SearchModel sm)
        {
            IQueryable<OutsourcePo> op = SoaSystemCenterDbContext.OutsourcePo.AsQueryable();

            if (sm.SearchItem!=null && sm.SearchItem.Count>0)
            {
                var tempDi = sm.SearchItem;

                string os_name = "os_name";
                if (tempDi.Keys.Contains(os_name)&&!string.IsNullOrEmpty(tempDi[os_name]))
                {
                    op = op.Where(c => c.os_name.Contains(tempDi[os_name]));
                }
                string os_tel = "os_tel";
                if (tempDi.Keys.Contains(os_tel)&&!string.IsNullOrEmpty(tempDi[os_tel]))
                {
                    op = op.Where(c => c.os_tel.Contains(tempDi[os_tel]));
                }
                
            }
            return Pager(op, sm);
        }
        /// <summary>
        /// 删除外协单位名称方法
        /// </summary>
        /// <param name="os_id"></param>
        /// <returns></returns>
        public bool DeleteOutsourcePo(int os_id)
        {
            if (os_id<0) return false;
                    var dbuser = SoaSystemCenterDbContext.OutsourcePo.FirstOrDefault(c => c.os_id == os_id);
                    if (dbuser == null)
                    {
                        return false;
                    }
                    //删除
                    string delsql = "delete from Outsource where os_id=@p0";
                    SoaSystemCenterDbContext.Database.ExecuteSqlRaw(delsql, dbuser.os_id);
                    SoaSystemCenterDbContext.SaveChanges(); 
            return true;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="os_id"></param>
        /// <returns></returns>
        public OutsourcePo GetOutsourcePoID(int os_id)
        {
            return SoaSystemCenterDbContext.OutsourcePo.FirstOrDefault(c => c.os_id == os_id);
        }

        public OutsourceDto GetOutsourceDtoByOsid(int os_id)
        {
            OutsourcePo outsourcePo = SoaSystemCenterDbContext.OutsourcePo.FirstOrDefault(c => c.os_id == os_id);
            OutsourceDto userDto = new OutsourceDto();
            ModelCopier.CopyModel(outsourcePo, userDto);
            userDto.roleList = _iroleDbRepository.GetRoleListByUserName(os_id);
            return userDto;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public bool UpdateOutsourcePo(OutsourcePo op)
        {
            if (op == null) return false;
                SoaSystemCenterDbContext.Update(op);
                SoaSystemCenterDbContext.SaveChanges();
            return true;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="op"></param>
        /// <returns></returns>
        public bool AddOutsourcePo(OutsourcePo op)
        {
            if (op == null) return false;
                SoaSystemCenterDbContext.Add(op);
                SoaSystemCenterDbContext.SaveChanges(); 
            return true;
            
        }

    }
  
}
